<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/vue.js"></script>
</head>
<body>
<div id="app">
    <input type="text" v-model="searchName">
    <ul>
        <li v-for="(item,index) in filterPersons" :key="index">
            {{index}}----{{item.name}}----{{item.age}}
        </li>
    </ul>
    <button @click="setOrderType(1)">年龄升序</button>
    <button @click="setOrderType(2)">年龄降序</button>
    <button @click="setOrderType(0)">原本顺序</button>
</div>
<script>
    var vm = new Vue({
        el:'#app',
        data:{
            searchName:'',
            orderType:0,  // 0代表原本 1代表升序 2代表降序
            persons:[
                {name:'TOM',age:18},
                {name:'JACK',age:53},
                {name:'BOB',age:16},
                {name:'ROSE',age:22},
            ]
        },
        methods:{},
        computed:{
            filterPersons(){
                console.log(this)
                // 取出相关的数据
                const {searchName, persons, orderType} = this
                let fPersons;
                fPersons=persons.filter(item=>item.name.indexOf(searchName)!==-1)
                // 排序
                if (orderType!==0){
                    fPersons.sort(function (p1,p2) {
                        // 如果返回负数 p1在前
                        // 如果返回正数 p2在前
                        if (orderType===2){
                            return p2.age-p1.age
                        }else {
                            return p1.age-p2.age
                        }

                    })
                }
                return fPersons
            }
        },
        methods:{
            setOrderType(order){
                this.orderType=order
            }
        }
    })
</script>
</body>
</html>
